﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComTest;

namespace GUI
{
    public enum Rooms { Room0, Room1, Room2, All }
    public class Engine
    {
        private FormMain _form;

        public Engine(FormMain form)
        {
            _form = form;

            SerialPortHandler.Instance.DataReceived += _dataReceived;
            _form.buttonRoomRefreshClick += _roomRefresh;
            _form.buttonRoomSendClick += _sendDesiredTempToRoom;
            _form.buttonTimeSendClick += _sendTime;

            _roomRefresh(Rooms.All);
            _desiredRefresh(Rooms.All);
            _timeRefresh();
        }        

        private void _roomRefresh(Rooms room)
        {
            switch (room)
            {
                case Rooms.Room0:
                    SerialPortHandler.Instance.Write("T:0\n");
                    break;
                case Rooms.Room1:
                    SerialPortHandler.Instance.Write("T:1\n");
                    break;
                case Rooms.Room2:
                    SerialPortHandler.Instance.Write("T:2\n");
                    break;
                case Rooms.All:
                    SerialPortHandler.Instance.Write("T:?\n");
                    break; 
            }
            _desiredRefresh(room);
            _timeRefresh();
        }

        private void _desiredRefresh(Rooms room)
        {
            switch (room)
            {
                case Rooms.Room0:
                    SerialPortHandler.Instance.Write("D:0\n");
                    break;
                case Rooms.Room1:
                    SerialPortHandler.Instance.Write("D:1\n");
                    break;
                case Rooms.Room2:
                    SerialPortHandler.Instance.Write("D:2\n");
                    break;
                case Rooms.All:
                    SerialPortHandler.Instance.Write("D:?\n");
                    break;
            }
        }

        private void _sendDesiredTempToRoom(Rooms room, string msg)
        {
            switch (room)
            {
                case Rooms.Room0:
                    SerialPortHandler.Instance.Write("DS:0:" + msg + "\n");
                    break;
                case Rooms.Room1:
                    SerialPortHandler.Instance.Write("DS:1:" + msg + "\n");
                    break;
                case Rooms.Room2:
                    SerialPortHandler.Instance.Write("DS:2:" + msg + "\n");
                    break;
            }
            _desiredRefresh(room);
        }

        private void _timeRefresh()
        {
            SerialPortHandler.Instance.Write("C\n");
        }

        private void _sendTime(DateTime date, DateTime time)
        {
            string[] dt = date.ToShortDateString().Split('-');
            dt[0] = dt[0].Remove(0, 2);

            string[] t = time.ToLongTimeString().Split(':');
            SerialPortHandler.Instance.Write("CS:" + dt[0] + ":" + dt[1] + ":" + dt[2] + ":" + t[0] + ":" + t[1] + ":" + t[2] + "\n");
            _timeRefresh();
        }

        private void _dataReceived(string receivedMessage)
        {
            receivedMessage = receivedMessage.TrimEnd();
            string[] msg = receivedMessage.Split(':');

            switch (msg[0])
            {
                case "T":
                    if (msg[1] == "?")
                    {
                        _form.Invoke(new RefreshRoomDelegate(_form.RefreshRoomTemperature), 0, msg[2]);
                        _form.Invoke(new RefreshRoomDelegate(_form.RefreshRoomTemperature), 1, msg[3]);
                        _form.Invoke(new RefreshRoomDelegate(_form.RefreshRoomTemperature), 2, msg[4]);
                    }
                    else
                    {
                        _form.Invoke(new RefreshRoomDelegate(_form.RefreshRoomTemperature), Convert.ToInt32(msg[1]), msg[2]);
                    }
                    break;
                case "D":
                    if (msg[1] == "?")
                    {
                        _form.Invoke(new RefreshRoomDelegate(_form.RefreshDesiredTemperature), 0, msg[2]);
                        _form.Invoke(new RefreshRoomDelegate(_form.RefreshDesiredTemperature), 1, msg[3]);
                        _form.Invoke(new RefreshRoomDelegate(_form.RefreshDesiredTemperature), 2, msg[4]);
                    }
                    else
                    {
                        _form.Invoke(new RefreshRoomDelegate(_form.RefreshDesiredTemperature), Convert.ToInt32(msg[1]), msg[2]);
                    }
                    break;
                case "C":
                    _form.Invoke(new RefreshTimeDelegate(_form.RefreshTime), msg[1], msg[2], msg[3], msg[4], msg[5], msg[6]);
                    break;
            }
        }
    }
}
